/**
 * 安全风险管控
 */
<template>
	<vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="keyword">
							<template #label><vp-label zh="关键字" en="Keyword"/></template>
							<el-input v-model="searchForm.keyword" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="cjrq">
							<template #label><vp-label zh="创建日期" en="Creation Date"/></template>
							<el-date-picker
								v-model="searchForm.cjrq"
								type="daterange"
								start-placeholder="开始日期/Start Date"
								end-placeholder="结束日期/End Date"
								value-format="YYYY-MM-DD"
								unlink-panels
								range-separator="-"
								class="w100"
							>
							</el-date-picker>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="zdbsList">
							<template #label><vp-label zh="重大安全隐患" en="Major Hazard" /></template>
							<el-select v-model="searchForm.zdbsList" multiple collapse-tags>
								<el-option label="是 YES" :key="1" :value="1"></el-option>
								<el-option label="否 NO" :key="0" :value="0"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="bmidList">
							<template #label><vp-label zh="管理主责部门" en="Resp. Dept" /></template>
							<DepartmentMultSelectTree  :treeDefaultChecked="searchForm.treeDefaultChecked"
									:selectedText="searchForm.departmentDesc" @treeSelected="handleDepartmentSelected"
									class="w100"></DepartmentMultSelectTree>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="lyList">
							<template #label><vp-label zh="危险源来源" en="Source"/></template>
							<DictMulitSelect 
								:lxid='2008'
								:selectedId='searchForm.lyList'
								@dictMulitSelected='(data: any) => {searchForm.lyList = data}'
								class="w100"
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="fxpjfjList">
							<template #label><vp-label zh="风险评价分级" en="Risk Classification" /></template>
							<el-select v-model="searchForm.fxpjfjList" filterable multiple  collapse-tags placeholder="请选择" :class="w100">
								<el-option label="1A" key="1A" value="1A"></el-option>
								<el-option label="1B" key="1B" value="1B"></el-option>
								<el-option label="1C" key="1C" value="1C"></el-option>
								<el-option label="1D" key="1D" value="1D"></el-option>
								<el-option label="1E" key="1E" value="1E"></el-option>
								<el-option label="2A" key="2A" value="2A"></el-option>
								<el-option label="2B" key="2B" value="2B"></el-option>
								<el-option label="2C" key="2C" value="2C"></el-option>
								<el-option label="2D" key="2D" value="2D"></el-option>
								<el-option label="2E" key="2E" value="2E"></el-option>
								<el-option label="3A" key="3A" value="3A"></el-option>
								<el-option label="3B" key="3B" value="3B"></el-option>
								<el-option label="3C" key="3C" value="3C"></el-option>
								<el-option label="3D" key="3D" value="3D"></el-option>
								<el-option label="3E" key="3E" value="3E"></el-option>
								<el-option label="4A" key="4A" value="4A"></el-option>
								<el-option label="4B" key="4B" value="4B"></el-option>
								<el-option label="4C" key="4C" value="4C"></el-option>
								<el-option label="4D" key="4D" value="4D"></el-option>
								<el-option label="4E" key="4E" value="4E"></el-option>
								<el-option label="5A" key="5A" value="5A"></el-option>
								<el-option label="5B" key="5B" value="5B"></el-option>
								<el-option label="5C" key="5C" value="5C"></el-option>
								<el-option label="5D" key="5D" value="5D"></el-option>
								<el-option label="5E" key="5E" value="5E"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="ztList">
							<template #label><vp-label zh="状态" en="Status"/></template>
							<VpStatusMultSelect v-model="searchForm.ztList" />
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event"/>
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #actions>
			<vp-btn-add v-if="jb == 10" v-auth="'qualitySafetyDeptDangerousSrc.add'" @click="handleAdd"/>
			<vp-btn-add v-if="jb == 20" v-auth="'qualitySafetyOrgDangerousSrc.add'" @click="handleAdd"/>
			<vp-btn-export @click="handleExport"/>
		</template>
		<template #table>
			<vp-vxe-table
				:id="`${props.jb === 10 ? 'qualitySafetyDeptDangerousSrcTable' : 'qualitySafetyOrgDangerousSrcTable'}`"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column sortable field="bh" title="危险源编号 Hazard No." show-overflow :width="jb == 10 ? '190' : '150'">
					<template #header><vp-label zh="危险源编号" en="Hazard No."/></template>
				</vxe-column>
				<vxe-column sortable field="mc" title="危险源名称 Hazard Name" show-overflow width="250">
					<template #header><vp-label zh="危险源名称" en="Hazard Name"/></template>
					<template #default="scope">
						<span v-html="markTextColor(searchForm.keyword, scope.row.mc)"/>
					</template>
				</vxe-column>
				<vxe-column sortable field="zrbmmc" title="管理主责部门 Resp. Dept" show-overflow width="140">
					<template #header><vp-label zh="管理主责部门" en="Resp. Dept"/></template>
				</vxe-column>
				<vxe-column sortable field="zdbs" title="重大危险源 Major Hazard" show-overflow width="140">
					<template #header><vp-label zh="重大危险源" en="Major Hazard"/></template>
					<template #default="scope">
						{{scope.row.zdbs == 1 ? '是 YES' : scope.row.zdbs == 0 ? '否 NO' : ''}}
					</template>
				</vxe-column>
				<vxe-colgroup field="wxysb" title="危险源识别 Hazard Identification" width="150" align="center">
					<template #header><vp-label zh="危险源识别" en="Hazard Identification"/></template>
					<vxe-column field="ly" title="危险源来源 Source" show-overflow width="120">
						<template #header><vp-label zh="危险源来源" en="Source"/></template>
					</vxe-column>
					<vxe-column field="kndzdhg" title="可能导致的后果 Pot. Consequences" show-overflow width="150">
						<template #header><vp-label zh="可能导致的后果" en="Pot. Consequences"/></template>
					</vxe-column>
				</vxe-colgroup>
				<vxe-colgroup field="fxfxhfxpjfj" title="风险分析和风险评价分级 Risk Analysis and Risk Assessment Classification" width="150" align="center">
					<template #header><vp-label zh="风险分析和风险评价分级" en="Risk Analysis and Risk Assessment Classification"/></template>
					<vxe-column field="xyfxkzcs" title="现有风险控制措施 Exist. Risk Ctrl. Meas." show-overflow width="150">
						<template #header><vp-label zh="现有风险控制措施" en="Exist. Risk Ctrl. Meas."/></template>
					</vxe-column>
					<vxe-column field="fxfjKnx" title="可能性 Possibility" show-overflow width="100">
						<template #header><vp-label zh="可能性" en="Possibility"/></template>
					</vxe-column>
					<vxe-column field="fxfjYzx" title="严重性 Severity" show-overflow width="100">
						<template #header><vp-label zh="严重性" en="Severity"/></template>
					</vxe-column>
					<vxe-column field="fxz1" title="风险值 Risk Value" show-overflow width="120">
						<template #header><vp-label zh="风险值" en="Risk Value"/></template>
						<template #default="scope">
							{{scope.row.fxfjKnx && scope.row.fxfjYzx ? scope.row.fxfjKnx + scope.row.fxfjYzx : ''}}
						</template>
					</vxe-column>
					<vxe-column field="fxpjfj1" title="风险评价分级 Risk Classif." show-overflow width="130">
						<template #header><vp-label zh="风险评价分级" en="Risk Classif."/></template>
						<template #default="{row}">
							<span :class="formatFxpjfj(row.fxfjKnx, row.fxfjYzx)">
								<span v-if="formatFxpjfj(row.fxfjKnx, row.fxfjYzx) === 'fxpjfj_1'">可接受风险 Acceptable Risk</span>
								<span v-if="formatFxpjfj(row.fxfjKnx, row.fxfjYzx) === 'fxpjfj_2'">缓解后可接受风险 Acceptable Risk After Mitigation</span>
								<span v-if="formatFxpjfj(row.fxfjKnx, row.fxfjYzx) === 'fxpjfj_3'">不可接受风险 Unacceptable Risk</span>
								<span v-if="formatFxpjfj(row.fxfjKnx, row.fxfjYzx) === 'fxpjfj_unkown'"></span>
							</span>
						</template>
					</vxe-column>
				</vxe-colgroup>
				<vxe-column sortable field="fxkzcs" title="风险控制措施 Risk Control Measures" show-overflow width="200">
					<template #header><vp-label zh="风险控制措施" en="Risk Control Measures"/></template>
				</vxe-column>
				<vxe-colgroup field="syfx" title="剩余风险 Residual Risk" width="180" align="center">
					<template #header><vp-label zh="剩余风险" en="Residual Risk"/></template>
					<vxe-column field="syfxfjKnx" title="可能性 Possibility" show-overflow width="150">
						<template #header><vp-label zh="可能性" en="Possibility"/></template>
					</vxe-column>
					<vxe-column field="syfxfjYzx" title="严重性 Severity" show-overflow width="100">
						<template #header><vp-label zh="严重性" en="Severity"/></template>
					</vxe-column>
					<vxe-column field="fxz2" title="风险值 Risk Value" show-overflow width="120">
						<template #header><vp-label zh="风险值" en="Risk Value"/></template>
						<template #default="scope">
							{{scope.row.syfxfjKnx && scope.row.syfxfjYzx ? scope.row.syfxfjKnx + scope.row.syfxfjYzx : ''}}
						</template>
					</vxe-column>
					<vxe-column field="fxpjfj2" title="风险评价分级 Risk Classif." show-overflow width="130">
						<template #header><vp-label zh="风险评价分级" en="Risk Classif."/></template>
						<template #default="{row}">
							<span :class="formatFxpjfj(row.syfxfjKnx, row.syfxfjYzx)">
								<span v-if="formatFxpjfj(row.syfxfjKnx, row.syfxfjYzx) === 'fxpjfj_1'">可接受风险 Acceptable Risk</span>
								<span v-if="formatFxpjfj(row.syfxfjKnx, row.syfxfjYzx) === 'fxpjfj_2'">缓解后可接受风险 Acceptable Risk After Mitigation</span>
								<span v-if="formatFxpjfj(row.syfxfjKnx, row.syfxfjYzx) === 'fxpjfj_3'">不可接受风险 Unacceptable Risk</span>
								<span v-if="formatFxpjfj(row.syfxfjKnx, row.syfxfjYzx) === 'fxpjfj_unkown'"></span>
							</span>
						</template>
					</vxe-column>
					<vxe-column field="ysxdbxybs" title="是否衍生新的危险源 Is New Hazard is Gen." show-overflow width="180">
						<template #header><vp-label zh="是否衍生新的危险源" en="Is New Hazard is Gen."/></template>
						<template #default="scope">
							{{scope.row.ysxdbxybs == 1 ? '是 YES' : scope.row.ysxdbxybs == 0 ? '否 NO' : ''}}
							<el-link type="info"  class="foc-link" v-if="scope.row.ysxdbxybs == 1"  @click="handleView(scope.row.xdwxyid)">{{ scope.row.xdwxybh}}</el-link>
						</template>
					</vxe-column>
				</vxe-colgroup>
				<vxe-column sortable field="cjbm" title="创建部门 Creation Dept." show-overflow width="140">
					<template #header><vp-label zh="创建部门" en="Creation Dept."/></template>
				</vxe-column>
				<vxe-column sortable field="cjr" title="创建人 Creator" show-overflow width="130">
					<template #header><vp-label zh="创建人" en="Creator"/></template>
				</vxe-column>
				<vxe-column sortable field="cjsj" title="创建时间 Creation Date" show-overflow width="130">
					<template #header><vp-label zh="创建时间" en="Creation Date"/></template>
				</vxe-column>
				<vxe-column sortable field="zt" title="状态 Status" show-overflow width="110">
					<template #header><vp-label zh="状态" en="Status"/></template>
					<template #default="scope">
						{{ getStatusDesc(scope.row.zt) }}
					</template>
				</vxe-column>
				<vxe-column sortable field="lydj" title="来源单据 Source Doc." show-overflow width="200">
					<template #header><vp-label zh="来源单据" en="Source Doc."/></template>
					<template #default="scope">
						<el-link v-if="scope.row.glaqyhid" type="info" class="foc-link" :underline="false"  @click="handleViewGlyh(scope.row)">由部门转入:{{ scope.row.glaqyhbh}}</el-link>
						<el-link v-else-if="scope.row.glzgfkdid" type="info" class="foc-link" :underline="false"  @click="handleViewZgfkd(scope.row)">整改反馈单:{{ scope.row.zgfkdbh}}</el-link>
						<el-link v-else-if="scope.row.glzybgid" type="info" class="foc-link" :underline="false"  @click="handleViewZybg(scope.row)">{{scope.row.lylx === 20 ? '自愿报告' : '举报信息'}}:{{ scope.row.zybgbh}}</el-link>
						<span v-else>手工录入</span>
					</template>
				</vxe-column>
				<vxe-column field="operation" title="操作 Operations" fixed="right" :width="jb == 10 ? 300 : 200">
					<template #header><vp-label-operations/></template>
					<template #default="scope">
						<el-space>
							<vp-btn-edit v-if="scope.row.zt == 10 && jb == 10" text v-auth="'qualitySafetyDeptDangerousSrc.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt == 10 && jb == 10" text v-auth="'qualitySafetyDeptDangerousSrc.delete'" @click="handleDelete(scope.row)"/>
							<vp-btn-discard v-if="(scope.row.zt === 88 || scope.row.zt === 89) && jb == 10" text v-auth="'qualitySafetyDeptDangerousSrc.discard'" @click="handleCancle(scope.row)"/>
							<vp-btn-edit v-if="scope.row.zt == 10 && jb == 20" text v-auth="'qualitySafetyOrgDangerousSrc.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt == 10 && jb == 20" text v-auth="'qualitySafetyOrgDangerousSrc.delete'" @click="handleDelete(scope.row)"/>
							<vp-btn-discard v-if="(scope.row.zt === 88 || scope.row.zt === 89) && jb == 20" text v-auth="'qualitySafetyOrgDangerousSrc.discard'" @click="handleCancle(scope.row)"/>
							<vp-btn type="primary" v-if="scope.row.zt == 89  && scope.row.existGl == 0 && scope.row.jb == 10" text v-auth="'qualitySafetyDeptDangerousSrc.edit'" @click="handle2Company(scope.row)">
								<vp-label zh="纳入公司级清单" en="Included in Comp."/>
							</vp-btn>
							<vp-btn type="primary" v-if="scope.row.zt == 89 && jb == 10" text v-auth="'qualitySafetyDeptDangerousSrc.edit'" @click="handleForceEdit(scope.row)">
								<vp-label zh="强制修改" en="Mandatory Rev."/>
							</vp-btn>
							<vp-btn type="primary" v-if="scope.row.zt == 89 && jb == 20" text v-auth="'qualitySafetyOrgDangerousSrc.edit'" @click="handleForceEdit(scope.row)">
								<vp-label zh="强制修改" en="Mandatory Rev."/>
							</vp-btn>
							<!-- <vp-btn-print text @click="handlePrint(scope.row)"/> -->
							<vp-btn-view text @click="handleView(scope.row.id)"/>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<EditDeptDangerousSrc ref="editDialogRef" @formSubmited="handleFormSubmited"></EditDeptDangerousSrc>
	</vp-main-page>
</template>

<script setup lang="ts" name="dangerousSrcCommon">
import { ref, reactive, onMounted } from 'vue';
import request from '/@/utils/request';
import { markTextColor } from '/@/utils/toolsValidate';
import EditDeptDangerousSrc from "./component/EditDeptDangerousSrc.vue";
import router from '/@/router';
import DictMulitSelect from "/@/components/vp/dict/VpDictSelectMulit.vue";
import DepartmentMultSelectTree from '/@/components/vp/department/VpDepartmentMultSelectTree.vue';
import { DEFAULT_PAGE } from '/@/utils/page';
import VpMessageBox from '/@/components/vp/VpMessageBox';
import commonFunction from '/@/utils/commonFunction';
import VpStatusMultSelect from "/@/components/vp/status/VpStatusMultSelect.vue";

const searchForm = reactive({
	keyword: "",
	ztList: [10,88,89],
	fxrq:[],
	zgbmmc: "",
	yhlbList: [],
	lyList: [],
	gbbsList: [],
	zdbsList: [],
	departmentDesc: '',
	treeDefaultChecked: [],
	bmidList: [],
	jb: 10, // 级别 10部门 20公司
	sygn: 41,
});

const props = defineProps({
	jb: {
		type: Number,
	},
	sygn: {
		type: String,
	},
	type: {
		type: String,
	},
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

// 加载列表数据
const loadDataList = () => {
	loading.value = true
	request.get('/safe/deptDangerousSrc/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
const tableRef = ref();

// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
	searchForm.bmidList = [];
	searchForm.departmentDesc = '';
	searchForm.treeDefaultChecked = [];
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	if(props.jb){
		searchForm.jb = props.jb;
	}
	if(props.sygn){
		searchForm.sygn = props.sygn;
	}
	loadDataList();
});

const editDialogRef = ref();

const handleAdd = ()=>{
	editDialogRef.value.handleOpen('add','', props.jb || 10);
};
const handleView = (id:string)=>{
	router.push({
		name: 'viewDangerousSrc',
		params: {
			id: id,
		},
	});
}
const handleViewGlyh = (row:any)=>{
	router.push({
		name: 'viewHiddenTrouble',
		params: {
			id: row.glaqyhid,
		},
	});
}

const handleViewZgfkd = (row:any) =>{
	router.push({
	name: 'viewRectification',
	params: {
		id: row.glzgfkdid,
	},
	});
}  

const handleEdit = (row: any)=>{
	editDialogRef.value.handleOpen('edit', row.id, row.jb);
}

const handleForceEdit = (row: any)=>{
	editDialogRef.value.handleOpen('forceEdit', row.id, row.jb);
}

// 删除
const handleDelete = (row: any)=>{
	VpMessageBox.confirmDelete().then(()=>{
		loading.value = true;
		request.delete(`/safe/deptDangerousSrc/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 终止
const handleCancle = (row: any)=>{
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/safe/deptDangerousSrc/cancle/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

const handle2Company = (row: any)=>{
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/safe/deptDangerousSrc/company/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

const handleFormSubmited=()=>{
	loadDataList();
}

const {exportFile, printFile, getStatusDesc} = commonFunction();

const handleExport = () =>{
	if(!props.jb){
		return;
	}
	exportFile({
		url: `/safe/deptDangerousSrc/export`,
		params: {
			...searchForm,
		},
		fileName: props.jb == 10 ? '部门级安全风险管控.xlsx' : props.jb == 20 ? '公司级安全风险管控.xlsx' : "安全风险管控.xlsx",
	});
}
const handlePrint = (row: any) =>{
	printFile({
		url: `/safe/deptDangerousSrc/print/${row.id}`
	});
}

const handleDepartmentSelected = (list: any) => {
	let bmidList = [] as string[];
	list.forEach((obj: any) => {
		bmidList.push(obj.id);
	});
	searchForm.bmidList = bmidList;
	searchForm.departmentDesc = list.map((obj: any) => {
		return obj.label;
	}).join(", ");
}

const knxArray = ['1','2','3','4','5']; //可能性
const yzxArray = ['A','B','C','D','E']
/**
 * 格式化风险评价分级
 */
const formatFxpjfj = (knx:string, yzx: string)=>{
	let iKnx = knxArray.indexOf(knx);
	let iYzx = yzxArray.indexOf(yzx);
	if(iKnx <0 || iYzx <0){
		return 'fxpjfj_unkown';
	}

	//当可能性与严重性 序号相加大于等于8 时，风险评价分级为不可接受风险
	if(iKnx + iYzx + 2 >= 8){
		return 'fxpjfj_3';
	}

	//当可能性与严重性 序号相加小于等于4，并且不都等于2时 风险评价分级为不可接受风险
	if(iKnx + iYzx + 2 < 4 || (iKnx + iYzx + 2 == 4 && iKnx != iYzx)){
		return 'fxpjfj_1';
	}
	//其他情况为缓解后可接受风险
	return 'fxpjfj_2';
}  

const handleViewZybg = (row: any) =>{
	if(row.lylx == 20){
	router.push({
		name: 'viewVoluntaryOrgHandle',
		params: {
		id: row.glzybgmxid,
		},
	});
	}else {
	router.push({
		name: 'viewReportOrgHandle',
		params: {
		id: row.glzybgmxid,
		},
	});
	}
}
</script>
<style lang="scss" scoped>
:deep(.warn-collect) {
  color: red;
}

.fxpjfj_1 {
  font-weight: bold;
  font-size: large;
  color: green;
}
.fxpjfj_2 {
  font-weight: bold;
  font-size: large;
  color: orange;
}
.fxpjfj_3 {
  font-weight: bold;
  font-size: large;
  color: red;
}
</style>
